﻿// 功能：时间日期设置对话框
// 描述：
// 作者：温伟鹏
// 日期：2010/5/18 22:23:11

using System;
using System.Runtime.InteropServices;
using MeizuSDK.Drawing;

namespace MeizuSDK.Presentation
{
    /// <summary>
    /// 时间日期设置对话框
    /// </summary>
    public class SetDailyDialog
    {
        #region DllImport
        [DllImport("MZFCWrapper.dll")]
        private static extern IntPtr MzSetDailyCtor();

        [DllImport("MZFCWrapper.dll")]
        private static extern void MzSetDailyDispose(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void MzSetDailyCreate(IntPtr pObj, IntPtr hWnd);

        [DllImport("MZFCWrapper.dll")]
        private static extern void MzSetDailyEnableDisplayYearData(IntPtr pObj, bool bSetFlag);

        [DllImport("MZFCWrapper.dll")]
        private static extern uint MzSetDailyGetSystemTimeFormat(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool MzSetDailyIsDisplayYearData(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool MzSetDailyIsEnableChangeItemData(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern int MzSetDailyMaxDaysInMonth(IntPtr pObj, int iMonyhs, int Years);

        [DllImport("MZFCWrapper.dll")]
        private static extern void MzSetDailySetEnableChangeItemData(IntPtr pObj, bool bSetFlag);

        [DllImport("MZFCWrapper.dll")]
        private static extern void MzSetDailySetItemCounter(IntPtr pObj, int itemCounter);

        [DllImport("MZFCWrapper.dll")]
        private static extern void MzSetDailySetItem1Data(IntPtr pObj, int item1Data);

        [DllImport("MZFCWrapper.dll")]
        private static extern void MzSetDailySetItem2Data(IntPtr pObj, int item2Data);

        [DllImport("MZFCWrapper.dll")]
        private static extern void MzSetDailySetItem3Data(IntPtr pObj, int item3Data);

        [DllImport("MZFCWrapper.dll")]
        private static extern void MzSetDailySetItem1Min(IntPtr pObj, int item1Min);

        [DllImport("MZFCWrapper.dll")]
        private static extern void MzSetDailySetItem2Min(IntPtr pObj, int item2Min);

        [DllImport("MZFCWrapper.dll")]
        private static extern void MzSetDailySetItem3Min(IntPtr pObj, int item3Min);

        [DllImport("MZFCWrapper.dll")]
        private static extern void MzSetDailySetItem1Max(IntPtr pObj, int item1Max);

        [DllImport("MZFCWrapper.dll")]
        private static extern void MzSetDailySetItem2Max(IntPtr pObj, int item2Max);

        [DllImport("MZFCWrapper.dll")]
        private static extern void MzSetDailySetItem3Max(IntPtr pObj, int item3Max);

        [DllImport("MZFCWrapper.dll")]
        private static extern void MzSetDailySetItem1Width(IntPtr pObj, int item1Width);

        [DllImport("MZFCWrapper.dll")]
        private static extern void MzSetDailySetItem2Width(IntPtr pObj, int item2Width);

        [DllImport("MZFCWrapper.dll")]
        private static extern void MzSetDailySetItem3Width(IntPtr pObj, int item3Width);

        [DllImport("MZFCWrapper.dll")]
        private static extern void MzSetDailySetItem1Text(IntPtr pObj, [MarshalAs(UnmanagedType.LPWStr)]string text);

        [DllImport("MZFCWrapper.dll")]
        private static extern void MzSetDailySetItem2Text(IntPtr pObj, [MarshalAs(UnmanagedType.LPWStr)]string text);

        [DllImport("MZFCWrapper.dll")]
        private static extern void MzSetDailySetItem3Text(IntPtr pObj, [MarshalAs(UnmanagedType.LPWStr)]string text);

        [DllImport("MZFCWrapper.dll")]
        private static extern void MzSetDailySetOkText(IntPtr pObj, [MarshalAs(UnmanagedType.LPWStr)]string text);

        [DllImport("MZFCWrapper.dll")]
        private static extern void MzSetDailySetCancelText(IntPtr pObj, [MarshalAs(UnmanagedType.LPWStr)]string text);

        [DllImport("MZFCWrapper.dll")]
        private static extern int MzSetDailyGetItem1Data(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern int MzSetDailyGetItem2Data(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern int MzSetDailyGetItem3Data(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern int MzSetDailyDoModal(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void MzSetDailyAnimateWindow(IntPtr pObj, uint nAnimType, bool bShow);

        #endregion

        #region 私有变量
        private IntPtr handle;
        private DailySetting setting;
        private AnimationType animateIn;
        private AnimationType animateOut;
        private bool isShowing;
        private int selectedItem1Data;
        private int selectedItem2Data;
        private int selectedItem3Data;
        #endregion

        #region 公共属性
        /// <summary>
        /// 获取或设置对话框显示动画
        /// </summary>
        public AnimationType AnimateIn
        {
            get
            {
                return animateIn;
            }
            set
            {
                if (value != this.animateIn)
                {
                    this.animateIn = value;
                }
            }
        }
        /// <summary>
        /// 获取或设置对话框退出动画
        /// </summary>
        public AnimationType AnimateOut
        {
            get
            {
                return this.animateOut;
            }
            set
            {
                if (value != this.animateOut)
                {
                    this.animateOut = value;
                }
            }
        }
        /// <summary>
        /// 获取或设置对话框的设置项
        /// </summary>
        public DailySetting Setting
        {
            get
            {
                return setting;
            }
        }
        /// <summary>
        /// 获取第一列选中的值
        /// </summary>
        public int SelectedItem1Value
        {
            get
            {
                return selectedItem1Data;
            }
        }
        /// <summary>
        /// 获取第二列选中的值
        /// </summary>
        public int SelectedItem2Value
        {
            get
            {
                return selectedItem2Data;
            }
        }
        /// <summary>
        /// 获取第三列选中的值
        /// </summary>
        public int SelectedItem3Value
        {
            get
            {
                return selectedItem3Data;
            }
        }
        /// <summary>
        /// 获取或设置一个值，指示对话框是否显示年（如果设置为false，则第一列中显示项的索引值）
        /// </summary>
        public bool DisplayYear
        {
            get
            {
                return MzSetDailyIsDisplayYearData(handle);
            }
            set
            {
                MzSetDailyEnableDisplayYearData(handle, value);
            }
        }
        /// <summary>
        /// 获取或设置一个值，指示是否可以修改ItemValue
        /// </summary>
        public bool EnableChangeItemValue
        {
            get
            {
                return MzSetDailyIsEnableChangeItemData(handle);
            }
            set
            {
                MzSetDailySetEnableChangeItemData(handle, value);
            }
        }
        #endregion

        #region 构造函数

        public SetDailyDialog()
        {
            handle = MzSetDailyCtor();
            animateIn = AnimationType.Fade;
            animateOut = AnimationType.Fade;
            DisplayYear = true;
            setting = new DailySetting(this);
            isShowing = false;
        }
        #endregion

        #region 函数

        #region 设置函数
        /// <summary>
        /// 
        /// </summary>
        /// <param name="setting"></param>
        internal void SetItemCounter(int itemCount)
        {
            if (!isShowing)
                MzSetDailySetItemCounter(handle, itemCount);
        }

        internal void SetItem1Data(int data)
        {
            if (!isShowing)
                MzSetDailySetItem1Data(handle, data);
        }

        internal void SetItem2Data(int data)
        {
            if (!isShowing)
                MzSetDailySetItem2Data(handle, data);
        }

        internal void SetItem3Data(int data)
        {
            if (!isShowing)
                MzSetDailySetItem3Data(handle, data);
        }

        internal void SetItem1Min(int min)
        {
            if (!isShowing)
                MzSetDailySetItem1Min(handle, min);
        }

        internal void SetItem2Min(int min)
        {
            if (!isShowing)
                MzSetDailySetItem2Min(handle, min);
        }

        internal void SetItem3Min(int min)
        {
            if (!isShowing)
                MzSetDailySetItem3Min(handle, min);
        }

        internal void SetItem1Max(int max)
        {
            if (!isShowing)
                MzSetDailySetItem1Max(handle, max);
        }

        internal void SetItem2Max(int max)
        {
            if (!isShowing)
                MzSetDailySetItem2Max(handle, max);
        }

        internal void SetItem3Max(int max)
        {
            if (!isShowing)
                MzSetDailySetItem3Max(handle, max);
        }

        internal void SetItem1Width(int width)
        {
            if (!isShowing)
                MzSetDailySetItem1Width(handle, width);
        }

        internal void SetItem2Width(int width)
        {
            if (!isShowing)
                MzSetDailySetItem2Width(handle, width);
        }

        internal void SetItem3Width(int width)
        {
            if (!isShowing)
                MzSetDailySetItem3Width(handle, width);
        }

        internal void SetItem1Text(string text)
        {
            if (!isShowing)
                MzSetDailySetItem1Text(handle, text);
        }

        internal void SetItem2Text(string text)
        {
            if (!isShowing)
                MzSetDailySetItem2Text(handle, text);
        }

        internal void SetItem3Text(string text)
        {
            if (!isShowing)
                MzSetDailySetItem3Text(handle, text);
        }

        internal void SetOkText(string text)
        {
            if (!isShowing)
                MzSetDailySetOkText(handle, text);
        }

        internal void SetCancelText(string text)
        {
            if (!isShowing)
                MzSetDailySetCancelText(handle, text);
        }
        #endregion

        /// <summary>
        /// 获取指定年月的最大天数
        /// </summary>
        /// <param name="month">月份</param>
        /// <param name="year">年份</param>
        /// <returns></returns>
        public int MaxDaysInMonth(int month, int year)
        {
            return MzSetDailyMaxDaysInMonth(handle, month, year);
        }
        /// <summary>
        /// 显示日期时间设置对话框
        /// </summary>
        /// <returns></returns>
        public System.Windows.Forms.DialogResult ShowDialog(IntPtr hWnd)
        {
            isShowing = true;

            try
            {
                MzSetDailyCreate(handle, hWnd);

                MzSetDailyAnimateWindow(handle, (uint)animateIn, true);

                int id_ok = MzSetDailyDoModal(handle);

                System.Windows.Forms.DialogResult dialogResult = (System.Windows.Forms.DialogResult)id_ok;// id_ok=1表示点击了“确定”按钮

                if (dialogResult == System.Windows.Forms.DialogResult.OK) // 
                {
                    selectedItem1Data = MzSetDailyGetItem1Data(handle);
                    selectedItem2Data = MzSetDailyGetItem2Data(handle);
                    selectedItem3Data = MzSetDailyGetItem3Data(handle);
                }

                //MzSetDailyAnimateWindow(handle, (uint)animateOut, false);

                MzSetDailyDispose(handle);

                return dialogResult;
            }
            catch
            {
                return System.Windows.Forms.DialogResult.Cancel;
            }
            finally
            {
                isShowing = false;
            }
        }
        #endregion

    }

    /// <summary>
    /// 时间日期设置
    /// </summary>
    public class DailySetting
    {
        #region 私有变量
        int iItemCounter;
        int iItem1Data;
        int iItem2Data;
        int iItem3Data;
        int iItem1Width;
        int iItem2Width;
        int iItem3Width;
        int iItem1Min;
        int iItem2Min;
        int iItem3Min;
        int iItem1Max;
        int iItem2Max;
        int iItem3Max;

        String sItem1;
        String sItem2;
        String sItem3;

        String sBtnOK;
        String sBtnCancel;

        private SetDailyDialog owner;
        #endregion

        #region 公共属性
        /// <summary>
        /// 获取此时间日期设置对象所属的设置对话框
        /// </summary>
        public SetDailyDialog Owner
        {
            get
            {
                return owner;
            }
            internal set
            {
                this.owner = value;
            }
        }
        /// <summary>
        /// 获取或设置时间日期对话框的设置项数量。比如日期格式为3列，时间格式可以为2列也可以为3列。最大值为3
        /// </summary>
        public int ItemCount
        {
            get
            {
                return this.iItemCounter;
            }
            set
            {
                if (value != this.iItemCounter)
                {
                    if (value > 3)
                    {
                        value = 3;
                    }
                    this.iItemCounter = value;
                    this.Owner.SetItemCounter(value);
                }
            }
        }
        /// <summary>
        /// 获取或设置第一列的最大项数（年份从1923年开始计算。如果要将最大年份设置为2012年，则Item1MaxValue值应该设置为2012-1923）
        /// </summary>
        public int Item1Max
        {
            get
            {
                return this.iItem1Max;
            }
            set
            {
                if (value != this.iItem1Max)
                {
                    if (value < Item1Min)
                    {
                        value = Item1Min;
                    }
                    this.iItem1Max = value;
                    this.Owner.SetItem1Max(value);
                }
            }
        }
        /// <summary>
        /// 获取或设置第二列的最大项数
        /// </summary>
        public int Item2Max
        {
            get
            {
                return this.iItem2Max;
            }
            set
            {
                if (value != this.iItem2Max)
                {
                    if (value < Item2Min)
                    {
                        value = Item2Min;
                    }
                    this.iItem2Max = value;
                    this.Owner.SetItem2Max(value);

                }
            }
        }
        /// <summary>
        /// 获取或设置第三列的最大项数
        /// </summary>
        public int Item3Max
        {
            get
            {
                return this.iItem3Max;
            }
            set
            {
                if (value != this.iItem3Max)
                {
                    if (value < Item3Min)
                    {
                        value = Item3Min;
                    }
                    this.iItem3Max = value;
                    this.Owner.SetItem3Max(value);

                }
            }
        }
        /// <summary>
        /// 获取或设置第一列的当前索引（索引从0开始）
        /// </summary>
        public int Item1Index
        {
            get
            {
                return this.iItem1Data;
            }
            set
            {
                if (value != this.iItem1Data)
                {
                    if (value < Item1Min)
                    {
                        value = Item1Min;
                    }
                    else if (value > Item1Max)
                    {
                        value = Item1Max;
                    }

                    this.iItem1Data = value;
                    this.Owner.SetItem1Data(value);

                }
            }
        }
        /// <summary>
        /// 获取或设置第二列的当前索引（索引从0开始）
        /// </summary>
        public int Item2Index
        {
            get
            {
                return this.iItem2Data;
            }
            set
            {
                if (value != this.iItem2Data)
                {
                    if (value < Item2Min)
                    {
                        value = Item2Min;
                    }
                    else if (value > Item2Max)
                    {
                        value = Item2Max;
                    }

                    this.iItem2Data = value;
                    this.Owner.SetItem2Data(value);
                }
            }
        }
        /// <summary>
        /// 获取或设置第三列的当前索引（索引从0开始）
        /// </summary>
        public int Item3Index
        {
            get
            {
                return this.iItem3Data;
            }
            set
            {
                if (value != this.iItem3Data)
                {
                    if (value < Item3Min)
                    {
                        value = Item3Min;
                    }
                    else if (value > Item3Max)
                    {
                        value = Item3Max;
                    }

                    this.iItem3Data = value;
                    this.Owner.SetItem3Data(value);
                }
            }
        }
        /// <summary>
        /// 获取或设置第一列的最小项数
        /// </summary>
        public int Item1Min
        {
            get
            {
                return this.iItem1Min;
            }
            set
            {
                if (value != this.iItem1Min)
                {
                    if (value > Item1Max)
                    {
                        value = Item1Max - 1;
                    }
                    this.iItem1Min = value;
                    this.Owner.SetItem1Min(value);
                }
            }
        }
        /// <summary>
        /// 获取或设置第二列的最小项数
        /// </summary>
        public int Item2Min
        {
            get
            {
                return this.iItem2Min;
            }
            set
            {
                if (value != this.iItem2Min)
                {
                    if (value > Item2Max)
                    {
                        value = Item2Max - 1;
                    }
                    this.iItem2Min = value;
                    this.Owner.SetItem2Min(value);
                }
            }
        }
        /// <summary>
        /// 获取或设置第三列的最小项数
        /// </summary>
        public int Item3Min
        {
            get
            {
                return this.iItem3Min;
            }
            set
            {
                if (value != this.iItem3Min)
                {
                    if (value > Item3Max)
                    {
                        value = Item3Max - 1;
                    }
                    this.iItem3Min = value;
                    this.Owner.SetItem3Min(value);
                }
            }
        }
        /// <summary>
        /// 获取或设置第一列的宽度
        /// </summary>
        public int Item1Width
        {
            get
            {
                return this.iItem1Width;
            }
            set
            {
                if (value != this.iItem1Width)
                {
                    this.iItem1Width = value;
                    this.Owner.SetItem1Width(value);
                }
            }
        }
        /// <summary>
        /// 获取或设置第二列的宽度
        /// </summary>
        public int Item2Width
        {
            get
            {
                return this.iItem2Width;
            }
            set
            {
                if (value != this.iItem2Width)
                {
                    this.iItem2Width = value;
                    this.Owner.SetItem2Width(value);
                }
            }
        }
        /// <summary>
        /// 获取或设置第三列的宽度
        /// </summary>
        public int Item3Width
        {
            get
            {
                return this.iItem3Width;
            }
            set
            {
                if (value != this.iItem3Width)
                {
                    this.iItem3Width = value;
                    this.Owner.SetItem3Width(value);
                }
            }
        }
        /// <summary>
        /// 获取或设置第一列的显示文本
        /// </summary>
        public string Item1Text
        {
            get
            {
                return this.sItem1;
            }
            set
            {
                if (value != this.sItem1)
                {
                    this.sItem1 = value;
                    this.Owner.SetItem1Text(value);
                }
            }
        }
        /// <summary>
        /// 获取或设置第二列的显示文本
        /// </summary>
        public string Item2Text
        {
            get
            {
                return this.sItem2;
            }
            set
            {
                if (value != this.sItem2)
                {
                    this.sItem2 = value;
                    this.Owner.SetItem2Text(value);
                }
            }
        }
        /// <summary>
        /// 获取或设置第三列的显示文本
        /// </summary>
        public string Item3Text
        {
            get
            {
                return this.sItem3;
            }
            set
            {
                if (value != this.sItem3)
                {
                    this.sItem3 = value;
                    this.Owner.SetItem3Text(value);
                }
            }
        }
        /// <summary>
        /// 获取或设置对话框确定按钮显示的文本
        /// </summary>
        public string OkButtonText
        {
            get
            {
                return sBtnOK;
            }
            set
            {
                if (value != sBtnOK)
                {
                    this.sBtnOK = value;
                    this.Owner.SetOkText(value);
                }
            }
        }
        /// <summary>
        /// 获取或设置对话框取消按钮显示的文本
        /// </summary>
        public string CancelButtonText
        {
            get
            {
                return this.sBtnCancel;
            }
            set
            {
                if (value != sBtnCancel)
                {
                    this.sBtnCancel = value;
                    this.Owner.SetCancelText(value);
                }
            }
        }
        #endregion

        #region 构造函数

        internal DailySetting(SetDailyDialog dialog)
        {
            this.owner = dialog;
            this.Item1Width = this.Item2Width = this.Item3Width = 140;
        }
        #endregion
    }
}
